package com.zb.frame.base.config.shiro;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 *  Shiro配置
 * </p>
 *
 * @author bangZZ
 * @since 2020-06-04
 */
@Configuration
public class ShiroConfig {

    /**
     * 配置安全管理器
     */
    @Bean(name = "securityManager")
    public SecurityManager securityManager(ShiroRealm shiroRealm){
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(shiroRealm);
        return securityManager;
    }

    /**
     * 配置过滤器
     */
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager){
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        // 注入安全管理器(Shiro的核心安全接口)
        bean.setSecurityManager(securityManager);
        Map<String, Filter> filters = new LinkedHashMap<>();
        filters.put("intercept", new ShiroFilter());
        bean.setFilters(filters);
        Map<String, String> chain = new LinkedHashMap<String, String>();
        // 如若需要session 请去掉 noSessionCreation
        // 例如  chain.put("/login/**","anon");
        // 配置未认证可访问的资源
        chain.put("/login/**","noSessionCreation,anon");// 登录接口
        // swagger2 界面资源
        chain.put("/swagger/**", "noSessionCreation,anon");
        chain.put("/v2/api-docs", "noSessionCreation,anon");
        chain.put("/swagger-ui.html", "noSessionCreation,anon");
        chain.put("/webjars/**", "noSessionCreation,anon");
        chain.put("/swagger-resources/**", "noSessionCreation,anon");
        chain.put("/captcha.jpg","noSessionCreation,anon");
        // 必须登录和拥有对应资源权限
        chain.put("/**","noSessionCreation,intercept");
        bean.setFilterChainDefinitionMap(chain);
        return bean;
    }

    /**
     * 启用Shiro内部Bean生命周期管理
     * @return
     */
    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    /**
     * 开启Shiro注解
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
